//
//  XHRRegistController.m
//  快医
//
//  Created by 张世敏 on 16/7/6.
//
//

#import "XHRRegistController.h"

@interface XHRRegistController ()
/**
 *  手机号输入框
 */
@property (nonatomic,weak) UITextField *phoneTField;

/**
 *  验证码输入框
 */
@property (nonatomic,weak) UITextField *textTField;

/**
 *  获取验证码按钮
 */
@property (nonatomic,weak) UIButton *getTextBtn;

/**
 *  同意协议点选按钮
 */
@property (nonatomic,weak) UIButton *agreeProtocolBtn;

/**
 *  文字
 */
@property (nonatomic,weak) UILabel *protocalLabel;

/**
 *  用户协议按钮
 */
@property (nonatomic,weak) UIButton *userProtocolBtn;

/**
 *  注册按钮
 */
@property (nonatomic,weak) UIButton *registerBtn;

/**
 *  timerIndex--
 */
@property (nonatomic,assign) NSInteger timerIndex;

/**
 *  定时器
 */
@property (nonatomic,strong) NSTimer *timer;


@end

@implementation XHRRegistController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化界面
    [self initUI];
    
    //布局控件
    [self setupLayout];
}


//控件初始化
- (void)initUI {
    self.title = @"注册";
    self.view.backgroundColor = [UIColor whiteColor];
    
 
    
    
    //手机号tf
    UITextField *phoneTField = [[UITextField alloc] init];
    self.phoneTField = phoneTField;
    phoneTField.borderStyle = UITextBorderStyleRoundedRect;
    phoneTField.placeholder = @"请输入您的手机号";
    phoneTField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:phoneTField];
    
    
    
    //验证码tf
    UITextField *textTField = [[UITextField alloc] init];
    self.textTField = textTField;
    textTField.borderStyle = UITextBorderStyleRoundedRect;
    textTField.placeholder = @"请输入验证码";
    textTField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:textTField];
    
    
    
    //获取验证码Button
    UIButton *getTextBtn = [[UIButton alloc] init];
    self.getTextBtn = getTextBtn;
    [getTextBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    getTextBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [getTextBtn setBackgroundColor:[UIColor grayColor]];
    getTextBtn.layer.cornerRadius = 3;
    getTextBtn.clipsToBounds = YES;
    [self.view addSubview:getTextBtn];
    
    [getTextBtn addTarget:self action:@selector(didClickGetTextBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    //同意协议Button (圆圈)
    UIButton *agreeProtocolBtn = [[UIButton alloc] init];
    self.agreeProtocolBtn = agreeProtocolBtn;
    [agreeProtocolBtn setBackgroundImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [agreeProtocolBtn setBackgroundImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    [self.view addSubview:agreeProtocolBtn];
    
    [agreeProtocolBtn addTarget:self action:@selector(didClickAgreeProtocolBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    //同意协议Label
    UILabel *protocalLabel = [[UILabel alloc] init];
    self.protocalLabel = protocalLabel;
    [protocalLabel setText:@"本人同意并阅读"];
    [protocalLabel setFont:[UIFont systemFontOfSize:13]];
    [self.view addSubview:protocalLabel];
    
    
    
    //用户协议Button
    UIButton *userProtocolBtn = [[UIButton alloc] init];
    self.userProtocolBtn = userProtocolBtn;
    [userProtocolBtn setTitle:@"用户协议" forState:UIControlStateNormal];
    [userProtocolBtn setTitleColor:XHRColorWithRGB(95, 205, 255) forState:UIControlStateNormal];
    [self.view addSubview:userProtocolBtn];
    
    
    
    //注册Button
    UIButton *registerBtn = [[UIButton alloc] init];
    self.registerBtn = registerBtn;
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setBackgroundColor:XHRColorWithRGB(32, 198, 198)];
    [self.view addSubview:registerBtn];
    
    
}

#pragma mark
#pragma mark - 同意协议Button 监听
- (void)didClickAgreeProtocolBtn:(UIButton *)agreeButton{
    
    self.agreeProtocolBtn.selected = !agreeButton.isSelected;
    
}

#pragma mark
#pragma mark - 获取验证码Button 监听
- (void)didClickGetTextBtn:(UIButton *)getTextBtn{

    if(getTextBtn.isSelected){
        return;
    }
    getTextBtn.selected = YES;
    self.timerIndex = 10;
 
    [self setupTimer];
}

#pragma mark
#pragma mark - 定时器
- (void)setupTimer{
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showTimer) userInfo:nil repeats:YES];
    NSRunLoop *runLoop = [NSRunLoop mainRunLoop];
    [runLoop addTimer:timer forMode:NSRunLoopCommonModes];
    self.timer = timer;
}

#pragma mark
#pragma mark - 倒计时
- (void)showTimer{

    [self.getTextBtn setTitle:[NSString stringWithFormat:@"%ldS",self.timerIndex] forState:UIControlStateNormal];
    
    if(self.timerIndex == 0){
        
        [self.getTextBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.getTextBtn.selected = NO;
        [self.timer invalidate];
        self.timerIndex = 10;
        return;
    }
    
    self.timerIndex--;
    
}

//控件布局
- (void)setupLayout{
    
    //手机号tf
    [self.phoneTField makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.offset(60);
        make.leading.offset(20);
        make.trailing.offset(-20);
        make.height.equalTo(35);
    }];
    
    
    
    //获取验证码Button
    [self.getTextBtn makeConstraints:^(MASConstraintMaker *make) {
    
        make.top.equalTo(self.phoneTField.bottom).offset(30);
        make.width.equalTo(100);
        make.trailing.offset(-20);
        make.height.equalTo(35);
    }];
    
    
    
    //验证码tf
    [self.textTField makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.getTextBtn.top);
        make.leading.equalTo(self.phoneTField.leading);
        make.height.equalTo(35);
        make.trailing.equalTo(self.getTextBtn.leading).offset(-20);
    }];
    
    
    
    //同意协议Button  (圆圈)
    [self.agreeProtocolBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.textTField.bottom).offset(50);
        make.leading.offset(70);
        
        [self.agreeProtocolBtn sizeToFit];
    }];
    
    
    
    //同意协议Label
    [self.protocalLabel makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.agreeProtocolBtn.centerY);
        make.leading.equalTo(self.agreeProtocolBtn.trailing).offset(10);
    }];
    
    
    
    
    //用户协议Button
    [self.userProtocolBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.protocalLabel.centerY);
        make.leading.equalTo(self.protocalLabel.trailing).offset(10);
        [self.userProtocolBtn sizeToFit];
    }];
    
    
    
    //注册Button
    [self.registerBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.agreeProtocolBtn.bottom).offset(30);
        make.leading.equalTo(self.phoneTField.leading);
        make.trailing.equalTo(self.phoneTField.trailing);
        make.height.equalTo(45);
    }];

}
@end
